package org.tea.inventory.admin.web.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.admin.web.admin.BaseController;
import org.tea.inventory.base.response.BaseResponse;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.base.util.excel.ExportExcel;
import org.tea.inventory.domain.tea.Product;
import org.tea.inventory.domain.tea.WarehouseInfo;
import org.tea.inventory.domain.tea.WarehouseInfo;
import org.tea.inventory.domain.tea.WarehouseInventorySerialInfo;
import org.tea.inventory.service.IWarehouseInfoService;
import org.tea.inventory.service.IWarehouseInventorySerialService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓库信息 Controller
 * 
 * @author dongxb
 * @date 2025-04-14
 */
@Api(tags = "基础设置-仓库信息控制器")
@Slf4j
@RestController
@RequestMapping("/admin/warehouse-info")
@RequiredArgsConstructor
public class WarehouseInfoController extends BaseController
{
    @Autowired
    private IWarehouseInfoService warehouseInfoService;

    @Autowired
    private IWarehouseInventorySerialService warehouseInventorySerialService;

    /**
     * 查询仓库信息列表
     */
    @GetMapping("page")
    @ApiOperation(value = "查询仓库信息列表")
    @ApiResponses(
            @ApiResponse(code = 0, message = "成功", response = Product.class)
    )
    public BaseResponse page(WarehouseInfo warehouseInfo)
    {
        startPage();
        List<WarehouseInfo> list = warehouseInfoService.selectWarehouseInfoList(warehouseInfo);
        return getDataTable(list);
    }

    /**
     * 查询仓库信息列表(不分页)
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询仓库信息列表(不分页)")
    @ApiResponses(
            @ApiResponse(code = 0 , message = "成功" , response = WarehouseInfo.class)
    )
    public BaseResponse list(WarehouseInfo warehouseInfo) {
        List<WarehouseInfo> warehouseInfos = warehouseInfoService.selectWarehouseInfoList(warehouseInfo);
        return ResponseUtil.ok(warehouseInfos);
    }

    /**
     * 导出仓库信息列表
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出仓库信息列表")
    public void exportWarehouseInfo(WarehouseInfo warehouseInfo, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        String title = "仓库信息列表";
        String header = "仓库编号,仓库名称,管理员,联系电话,仓库地址,备注";
        String column = "warehouseCode,warehouseName,adminId,adminPhone,warehouseAddress,remarks";
        map.put("biaoti", title);
        map.put("header", header);
        map.put("column", column);
        try {
            List<WarehouseInfo> productCategories = warehouseInfoService.selectWarehouseInfoList(warehouseInfo);
            List<Map> list = JSONArray.parseArray(JSON.toJSONString(productCategories), Map.class);
            ExportExcel.export(request, response, list, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取仓库信息详细信息
     */

    @GetMapping("/info")
    @ApiOperation(value = "获取仓库信息详细信息")
    public BaseResponse info(Long id){
        if(id == null){
            return ResponseUtil.badArgument();
        }

        WarehouseInfo warehouseInfo = warehouseInfoService.selectWarehouseInfoById(id);
        return ResponseUtil.ok(warehouseInfo);
    }

    /**
     * 新增仓库信息
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增仓库信息")
    public BaseResponse add(@RequestBody WarehouseInfo warehouseInfo)
    {
        return ResponseUtil.ok(warehouseInfoService.insertWarehouseInfo(warehouseInfo));
    }

    /**
     * 修改仓库信息
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改仓库信息")
    public BaseResponse edit(@RequestBody WarehouseInfo warehouseInfo)
    {
        return ResponseUtil.ok(warehouseInfoService.updateWarehouseInfo(warehouseInfo));
    }

    /**
     * 删除仓库信息
     */
    @PostMapping(value = "/remove")
    @ApiOperation(value = "删除仓库信息")
    public BaseResponse remove(Long id)
    {
        return ResponseUtil.ok(warehouseInfoService.deleteWarehouseInfoById(id));
    }

    /**
     * 获取仓库中商品库存信息(分页)
     */
    @GetMapping("/info/inventory")
    @ApiOperation(value = "获取仓库中商品库存信息(分页)")
    @ApiResponses(
            @ApiResponse(code = 0, message = "成功", response = Product.class)
    )
    public BaseResponse getInventory(
            @ApiParam(value = "查询仓库id") Long warehouseId ,
            @ApiParam(value = "查询类型: 1 入库单、拆卸单; 2 出库单、调拨单、换货单、组装单、报损单、盘点单")
            String type,
            @ApiParam(value = "查询条件") String keyword,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit)
    {
        //startPage();
        List<WarehouseInventorySerialInfo> list = warehouseInventorySerialService.getWarehouseInventory(warehouseId , type , keyword);
        return getDataTablePage(list,page,limit);
    }

    /**
     * 获取仓库中商品信息(不分页)
     */
    @GetMapping("/info/product")
    @ApiOperation(value = "获取仓库中商品信息(不分页)")
    @ApiResponses(
            @ApiResponse(code = 0, message = "成功", response = Product.class)
    )
    public BaseResponse getProduct(
            @ApiParam(value = "查询仓库id") Long warehouseId ,
            String type)
    {
        List<WarehouseInventorySerialInfo> list = warehouseInventorySerialService.getWarehouseProduct(warehouseId);
        return ResponseUtil.ok(list);
    }

}
